var app = getApp();
Page({
  data: {
    goods: [],
    typeList: [],
    cart: {
      count: 0,
      total: 0,
      list: {}
    },
    showCartDetail: false
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    }) 
    var shopId = options.id;
    var that = this;
    for (var i = 0; i < app.globalData.shops.length; ++i) {
      if (app.globalData.shops[i].id == shopId) {
        this.setData({
          shop: app.globalData.shops[i]
        });
        break;
      }
    }
    wx.request({
      url: app.globalData.serverAddress + '/type/apis/getalltype',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          typeList: res.data,
          classifySeleted: 's' + res.data[0].type_id
        });
      }
    })
    wx.request({
      url: app.globalData.serverAddress + '/food/apis/getallfood',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res.data);
        res.data.forEach(function (food) {
          if ('http' != food.food_pic.substring(0, 4)) {
            food.food_pic = app.globalData.serverAddress + food.food_pic;
          }
          that.data.goods[food.food_id] = food;
        });
        that.setData({
          goods: that.data.goods
        });
      }
    })
  },
  onShow: function () {
  },
  tapAddCart: function (e) {
    this.addCart(e.target.dataset.id);
    console.log(e.target.dataset.id);
  },
  tapReduceCart: function (e) {
    this.reduceCart(e.target.dataset.id);
  },
  addCart: function (id) {
    var num = this.data.cart.list[id] || 0;
    this.data.cart.list[id] = num + 1;
    console.log(this.data.cart.list);
    this.countCart();
  },
  reduceCart: function (id) {
    var num = this.data.cart.list[id] || 0;
    if (num <= 1) {
      delete this.data.cart.list[id];
    } else {
      this.data.cart.list[id] = num - 1;
    }
    this.countCart();
  },
  countCart: function () {
    var count = 0,
      total = 0;
    for (var id in this.data.cart.list) {
      var goods = this.data.goods[id];

      console.log(this.data.goods);
      count += this.data.cart.list[id];
      total += goods.food_price * this.data.cart.list[id];
    }
    this.data.cart.count = count;
    this.data.cart.total = total;
    this.setData({
      cart: this.data.cart,
      goods: this.data.goods
    });
  },
  follow: function () {
    this.setData({
      followed: !this.data.followed
    });
  },
  onGoodsScroll: function (e) {

    if (e.detail.scrollTop > 10 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 10 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }

    var scale = e.detail.scrollWidth / 570,
      scrollTop = e.detail.scrollTop / scale,
      h = 0,
      classifySeleted,
      len = this.data.typeList.length;
    this.data.typeList.forEach(function (classify, i) {
      var _h = 70 + classify.goods.length * (46 * 3 + 20 * 2);
      if (scrollTop >= h - 100 / scale) {
        classifySeleted = 's' + classify.type_id;
      }
      h += _h;
    });
    this.setData({
      classifySeleted: classifySeleted
    });
  },
  tapClassify: function (e) {
    var id = e.target.dataset.id;
    this.setData({
      classifyViewed: id
    });
    var self = this;
    setTimeout(function () {
      self.setData({
        classifySeleted: id
      });
    }, 100);
  },
  showCartDetail: function () {
    this.setData({
      showCartDetail: !this.data.showCartDetail
    });
  },
  hideCartDetail: function () {
    this.setData({
      showCartDetail: false
    });
  },
  submit: function (e) {
    this.goAddress();
  },
  onShareAppMessage: function () {
    return {
      title: '华电外卖',
      desc: '华电人自己的外卖平台!',
      path: '/page/shop/shop'
    }
  },
  goAddress: function () {
    wx.navigateTo({ url: '/page/address/address?before=1' });
  },
  onPullDownRefresh: function () {
    var opt = { id: 1 }
    this.onLoad(opt);
    wx.stopPullDownRefresh()
  }
});

